package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.client.SearchServiceClient;
import cn.itsource.hrm.constants.CourseConstants;
import cn.itsource.hrm.context.LoginContext;
import cn.itsource.hrm.doc.CourseDoc;
import cn.itsource.hrm.domain.Course;
import cn.itsource.hrm.domain.CourseDetail;
import cn.itsource.hrm.domain.CourseMarket;
import cn.itsource.hrm.domain.LoginUser;
import cn.itsource.hrm.dto.CourseAddParamDTO;
import cn.itsource.hrm.exception.Assert;
import cn.itsource.hrm.exception.BusinessException;
import cn.itsource.hrm.mapper.CourseMapper;
import cn.itsource.hrm.service.ICourseDetailService;
import cn.itsource.hrm.service.ICourseMarketService;
import cn.itsource.hrm.service.ICourseService;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.vo.CrumbsVO;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaosang
 * @since 2022-04-10
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private ICourseMarketService courseMarketService;

    @Autowired
    private ICourseDetailService courseDetailService;

    @Autowired
    private SearchServiceClient searchServiceClient;


    /**
     * 添加课程：目的就是DTO中的三个对象保存到三个表
    * 1.参数判断
    * 2.条件判断
    * 3.保存课程，拿到ID
    * 4.保存课程营销
    * 把课程ID设置到营销表
    * 5.保存课程详情
    * 把课程ID设置到详情表
     *
     * @param dto ：参数对象
     */
    @Override
    @Transactional
    public void add(CourseAddParamDTO dto) {
        //* 1.参数判断 :JSR303
        Course course = dto.getCourse();
        CourseDetail courseDetail = dto.getCourseDetail();
        CourseMarket courseMarket = dto.getCourseMarket();

        //* 2.条件判断 ：比如：课程名不能重复
        String name = course.getName();

        //条件对象
        Wrapper<Course> wrapper = new EntityWrapper();
        wrapper.eq("name",name);
        Course courseFromDB = selectOne(wrapper);
        Assert.assertNull(courseFromDB,"课程名重复");

        //* 3.保存课程，拿到ID
        course.setStatus(CourseConstants.KEY_COURSE_STATUS_OFFLINE);
        LoginUser login = LoginContext.getLogin();

        course.setTenantId(login.getTenantId());
        course.setTenantName(login.getTenantName());
        course.setUserId(login.getId());
        course.setUserName(login.getUsername());
        insert(course);

        //* 4.保存课程营销
        //* 把课程ID设置到营销表
        courseMarket.setId(course.getId());
        courseMarketService.insert(courseMarket);

        //* 5.保存课程详情
        //* 把课程ID设置到详情表
        courseDetail.setId(course.getId());
        courseDetailService.insert(courseDetail);
    }


    /**
     * 课程上架
    * 1.参数判断
    * 2.条件判断：如果是未上架才能上架
    * 3.修改数据库课程状态为上架
    * 4.把课程数据存储到ES
    * 4.1.为ES服务编写保存课程的Controller
    * 4.2.为ES的Client模块编写Feign接口
    * 4.3.课程服务导入为ES的Client模块，集成Feign
     * @param courseId ：课程ID
     */
    @Override
    @Transactional
    public void onLineCourse(Long courseId) {
        //* 1.参数判断
        Assert.assertNotNull(courseId, "非法请求");
        //* 2.条件判断：如果是未上架才能上架
        Course course = selectById(courseId);
        Assert.assertNotNull(course , "非法请求");
        if(course.getStatus().intValue() != CourseConstants.KEY_COURSE_STATUS_OFFLINE){
            throw new BusinessException("课程不处于待上线状态");
        }
        //* 3.修改数据库课程状态为上架
        course.setStatus(CourseConstants.KEY_COURSE_STATUS_ONELINE);
        updateById(course);

        //* 4.把课程数据存储到ES

        CourseMarket courseMarket = courseMarketService.selectById(courseId);

        CourseDoc doc = new CourseDoc();
        BeanUtils.copyProperties(course ,doc);
        BeanUtils.copyProperties(courseMarket ,doc);
        doc.setChargeName(courseMarket.getCharge() == 1?"免费":"收费");
        doc.setOnlineTime(new Date());
        AjaxResult ajaxResult = searchServiceClient.add(doc);
        Assert.assertTrue(ajaxResult.isSuccess(),"课程上架失败");
    }


}
